/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:07
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.repository;

import com.joysuch.wwyt.edu.entity.EduCourse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.List;

/**
 * EduCourseDao
 *
 */
public interface EduCourseDao extends JpaRepository<EduCourse, Long>, QuerydslPredicateExecutor<EduCourse> {
    public Page<EduCourse> findAll(Specification<EduCourse> spec, Pageable pageable);

    public EduCourse save(EduCourse bean);

    public void delete(EduCourse bean);

    @Query("select count(m) from EduCourse m where code = ?1")
    long countOfCode(String code);

    @Query("select count(m) from EduCourse m where code = ?1 and id != ?2")
    long countOfCodeExcludeId(String code, Long id);

    @Query("select name from EduCourse m where id = ?1")
    public String getNameById(Long courseId);

    @Modifying
    @Query("update EduCourse set coursewareCount=(select count(*) from EduCourseCourseware where courseId=?1) where id = ?1")
    public void updateCoursewarecount(Long courseId);

    int countByName(String name);

    @Query(value = "select * from wwyt_edu_course  where DELETE_FLAG='0' and if(:name is null,1=1,name like %:name%)",nativeQuery = true)
    List<EduCourse> findByName(String name);
}
